package com.yt.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.yt.sys.models.service.ISysPermissionsService;
import com.yt.sys.models.bo.SysPermissionsSaveBO;
import com.yt.sys.models.bo.SysPermissionsBO;
import com.yt.sys.models.dto.SysPermissionsDTO;

/**
 * 权限详情控制器
 *
 * @author thekai
 * @Date 2025-10-23 11:31:56
 */

@RestController
@RequestMapping("/cms/sysPermissions")
@Slf4j
@Api(value = "SysPermissionsController",tags = "权限详情接口")
public class SysPermissionsController {

    @DubboReference
    private ISysPermissionsService sysPermissionsService;

    @ApiOperation(value = "添加权限详情",notes = "添加权限详情")
    @PostMapping(value = "/insert")
    public Result insert(SysPermissionsSaveBO saveBO) {
        boolean flag = sysPermissionsService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存权限详情",notes = "保存权限详情")
    public Result save(SysPermissionsSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改权限详情",notes = "修改权限详情")
    @PostMapping("/update")
    public Result update(SysPermissionsSaveBO saveBO){
        SysPermissionsDTO dto = new SysPermissionsDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = sysPermissionsService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         SysPermissionsDTO dto = new SysPermissionsDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = sysPermissionsService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除权限详情",notes = "删除权限详情")
    @ApiImplicitParam(name = "id",value = "权限详情id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = sysPermissionsService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除权限详情",notes = "批量删除权限详情")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        sysPermissionsService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysPermissionsDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysPermissionsDTO dto = sysPermissionsService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "权限详情列表",notes = "权限详情列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysPermissionsDTO>> queryPageList(SysPermissionsBO bo){
        Wrapper<SysPermissionsDTO> wrapper = new EntityWrapper<>();

                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysPermissionsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysPermissionsService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
